﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ejunkey.Models;
using System.Reflection;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Web.Helpers;
using System.IO;

namespace ejunkey.Controllers
{
    [Authorize]
    public class ProductController : Controller
    {
        private EjunkeyEntities db = new EjunkeyEntities();

        //
        // GET: /Product/
        [Authorize]
        public ViewResult Index()
        {
            var products = db.Products;//.Include(p => p.aspnet_Users).Include(p => p.Currency).Include(p => p.OtherProductsFile).Include(p => p.Package).Include(p => p.PromoteOtherProduct);
            return View(products.ToList());
        }

        //
        // GET: /Product/

        public ViewResult EditProductListing()
        {
            var productList = new List<SelectListItem>();

            foreach (Product p in db.Products)
            {
                productList.Add(new SelectListItem
                {
                    Value = p.ProductId.ToString(),
                    Text = "Product: " + p.ProductName + " " + p.Price.ToString()
                });
            }

            ProductListViewModel productListVM = new ProductListViewModel();

            productListVM.Products = productList;

            return View(productListVM);
        }

        //
        // GET: /Product/Details/5

        public ViewResult Details(Guid id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName");
            ViewBag.CurrencyId = new SelectList(db.Currencies, "CurrencyId", "CurrencyName");
            ViewBag.OtherProductFileId = new SelectList(db.OtherProductsFiles, "OtherProductFileId", "OtherProductFileId");
            ViewBag.PackageId = new SelectList(db.Packages, "PackageId", "PackageType");
            ViewBag.PromoteOtherProductId = new SelectList(db.PromoteOtherProducts, "PromoteOtherProductId", "PromoteOtherProductId");
            return View();
        } 

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                product.ProductId = Guid.NewGuid();
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", product.UserId);
            ViewBag.CurrencyId = new SelectList(db.Currencies, "CurrencyId", "CurrencyName", product.CurrencyId);
            ViewBag.OtherProductFileId = new SelectList(db.OtherProductsFiles, "OtherProductFileId", "OtherProductFileId", product.OtherProductFileId);
            ViewBag.PackageId = new SelectList(db.Packages, "PackageId", "PackageType", product.PackageId);
            ViewBag.PromoteOtherProductId = new SelectList(db.PromoteOtherProducts, "PromoteOtherProductId", "PromoteOtherProductId", product.PromoteOtherProductId);
            return View(product);
        }
        
        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Product product = db.Products.Find(id);
            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", product.UserId);
            ViewBag.CurrencyId = new SelectList(db.Currencies, "CurrencyId", "CurrencyName", product.CurrencyId);
            ViewBag.OtherProductFileId = new SelectList(db.OtherProductsFiles, "OtherProductFileId", "OtherProductFileId", product.OtherProductFileId);
            ViewBag.PackageId = new SelectList(db.Packages, "PackageId", "PackageType", product.PackageId);
            ViewBag.PromoteOtherProductId = new SelectList(db.PromoteOtherProducts, "PromoteOtherProductId", "PromoteOtherProductId", product.PromoteOtherProductId);
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", product.UserId);
            ViewBag.CurrencyId = new SelectList(db.Currencies, "CurrencyId", "CurrencyName", product.CurrencyId);
            ViewBag.OtherProductFileId = new SelectList(db.OtherProductsFiles, "OtherProductFileId", "OtherProductFileId", product.OtherProductFileId);
            ViewBag.PackageId = new SelectList(db.Packages, "PackageId", "PackageType", product.PackageId);
            ViewBag.PromoteOtherProductId = new SelectList(db.PromoteOtherProducts, "PromoteOtherProductId", "PromoteOtherProductId", product.PromoteOtherProductId);
            return View(product);
        }

        //
        // GET: /Product/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // GET: /Product/Delete/5
        public ActionResult DeleteProduct(Guid id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // POST: /Product/Delete/5
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("EditProductListing");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // Get: /Product/
        public ViewResult ProductConfiguration(Guid? id)
        {            
            if (id != null)
            {
                Product product = db.Products.Find(id);
                SessionSingleton.Current.SessionProduct = product;
                SessionSingleton.Current.SessionIsEdit = true;
            }
            else
            {
                Session.Abandon();
                Session.Clear();
            }

            return View(SessionSingleton.Current.SessionProduct);
        }

        //
        // POST: /Product/Submit
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "SubmitProductConfiguration")]
        public ActionResult SubmitProductConfiguration(Product product)
        {
            if (ModelState.IsValid)
            {
                if (SessionSingleton.Current.SessionProduct.IsMarketPalce != null)
                {
                    if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                    {
                        if (SessionSingleton.Current.SessionProduct.Details != null)
                            product.Details = SessionSingleton.Current.SessionProduct.Details;

                        var item = db.Products.Where(p => p.ProductId == SessionSingleton.Current.SessionProduct.ProductId).First();
                        SessionSingleton.Current.SessionProduct = (Product)item;
                        SessionSingleton.Current.SessionProduct.IsMarketPalce = true;

                        if (product.Details != null)
                        SessionSingleton.Current.SessionProduct.Details = product.Details;

                        if (product.Category1 != null)
                        SessionSingleton.Current.SessionProduct.Category1 = product.Category1;

                        if (product.Category2 != null)
                        SessionSingleton.Current.SessionProduct.Category2 = product.Category2;

                        if (product.Category3 != null)
                        SessionSingleton.Current.SessionProduct.Category3 = product.Category3;

                        if (product.Category4 != null)
                        SessionSingleton.Current.SessionProduct.Category4 = product.Category4;

                        if (product.Category5 != null)
                        SessionSingleton.Current.SessionProduct.Category5 = product.Category5;

                        if (product.DetailsUrl != null)
                        SessionSingleton.Current.SessionProduct.DetailsUrl = product.DetailsUrl;

                        if (product.DemoUrl != null)
                        SessionSingleton.Current.SessionProduct.DemoUrl = product.DemoUrl;

                        if (product.Tagline != null)
                        SessionSingleton.Current.SessionProduct.Tagline = product.Tagline;

                        if (product.ShortDescription != null)
                        SessionSingleton.Current.SessionProduct.ShortDescription = product.ShortDescription;
                    }
                }
                else
                {
                    SessionSingleton.Current.SessionProduct.IsMarketPalce = false;
                  
                }
                

                if(product.ProductName != null)
                SessionSingleton.Current.SessionProduct.ProductName = product.ProductName;

                if(product.IsSingleFile != null)
                SessionSingleton.Current.SessionProduct.IsSingleFile = product.IsSingleFile;

                if(product.IsRedirection != null)
                SessionSingleton.Current.SessionProduct.IsRedirection = product.IsRedirection;

                if(product.IsStoreCode != null)
                SessionSingleton.Current.SessionProduct.IsStoreCode = product.IsStoreCode;

                if (product.IsGenCode != null)
                SessionSingleton.Current.SessionProduct.IsGenCode = product.IsGenCode;

                if (product.IsAffectOrtherProducts != null)
                SessionSingleton.Current.SessionProduct.IsAffectOrtherProducts = product.IsAffectOrtherProducts;

                if (product.IsInventoryControl != null)
                SessionSingleton.Current.SessionProduct.IsInventoryControl = product.IsInventoryControl;

                if (product.IsTaxVAT != null)
                SessionSingleton.Current.SessionProduct.IsTaxVAT = product.IsTaxVAT;

                if (product.IsShipOrBuyerAddress != null)
                SessionSingleton.Current.SessionProduct.IsShipOrBuyerAddress = product.IsShipOrBuyerAddress;

                if (product.IsEditQuantity != null)
                SessionSingleton.Current.SessionProduct.IsEditQuantity = product.IsEditQuantity;

                if (product.IsEditPrice != null)
                SessionSingleton.Current.SessionProduct.IsEditPrice = product.IsEditPrice;

                if (product.IsVariants != null)
                SessionSingleton.Current.SessionProduct.IsVariants = product.IsVariants;

                if (product.IsVariations != null)
                SessionSingleton.Current.SessionProduct.IsVariations = product.IsVariations;

                if (product.IsSendTransaction != null)
                SessionSingleton.Current.SessionProduct.IsSendTransaction = product.IsSendTransaction;

                if (product.Price != null)
                SessionSingleton.Current.SessionProduct.Price = product.Price;

                if (product.EmailSubject != null)
                SessionSingleton.Current.SessionProduct.EmailSubject = product.EmailSubject;

                if (product.EmailMessage != null)
                SessionSingleton.Current.SessionProduct.EmailMessage = product.EmailMessage;

                if (product.IsReceiveCopy != null)
                SessionSingleton.Current.SessionProduct.IsReceiveCopy = product.IsReceiveCopy;

                if (product.IsEnableTemplateEmail != null)
                SessionSingleton.Current.SessionProduct.IsEnableTemplateEmail = product.IsEnableTemplateEmail;

                if (product.ItemNumber != null)
                SessionSingleton.Current.SessionProduct.ItemNumber = product.ItemNumber;

                if (product.MessageAfterPayment != null)
                SessionSingleton.Current.SessionProduct.MessageAfterPayment = product.MessageAfterPayment;
                                
                SessionSingleton.Current.SessionProduct.UserId = db.aspnet_Users.First().UserId;

                if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                {
                    db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                }
                else
                {
                    if (SessionSingleton.Current.SessionIsEdit == true)
                    {
                        db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                    }
                    else
                    {
                        SessionSingleton.Current.SessionProduct.ProductId = Guid.NewGuid();
                        db.Products.Add(SessionSingleton.Current.SessionProduct);
                    }
                }
                try
                { 
                    db.SaveChanges();
                }    // EXCEPTION!!!! WHAT 
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        { Debug.WriteLine(validationError.ErrorMessage); }
                    }

                    // rethrow, we want to fail this test if this happens
                    throw;
                }
                return RedirectToAction("GenerateButton");
            }

            return View(SessionSingleton.Current.SessionProduct);
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "Submit")]
        public ActionResult Submit(Product product)
        {
            if (ModelState.IsValid)
            {
                if (SessionSingleton.Current.SessionProduct.IsMarketPalce != null)
                {
                    if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                    {
                        if (SessionSingleton.Current.SessionProduct.Details != null)
                            product.Details = SessionSingleton.Current.SessionProduct.Details;

                        var item = db.Products.Where(p => p.ProductId == SessionSingleton.Current.SessionProduct.ProductId).First();
                        SessionSingleton.Current.SessionProduct = (Product)item;
                        SessionSingleton.Current.SessionProduct.IsMarketPalce = true;

                        if (product.Details != null)
                            SessionSingleton.Current.SessionProduct.Details = product.Details;

                        if (product.Category1 != null)
                            SessionSingleton.Current.SessionProduct.Category1 = product.Category1;

                        if (product.Category2 != null)
                            SessionSingleton.Current.SessionProduct.Category2 = product.Category2;

                        if (product.Category3 != null)
                            SessionSingleton.Current.SessionProduct.Category3 = product.Category3;

                        if (product.Category4 != null)
                            SessionSingleton.Current.SessionProduct.Category4 = product.Category4;

                        if (product.Category5 != null)
                            SessionSingleton.Current.SessionProduct.Category5 = product.Category5;

                        if (product.DetailsUrl != null)
                            SessionSingleton.Current.SessionProduct.DetailsUrl = product.DetailsUrl;

                        if (product.DemoUrl != null)
                            SessionSingleton.Current.SessionProduct.DemoUrl = product.DemoUrl;

                        if (product.Tagline != null)
                            SessionSingleton.Current.SessionProduct.Tagline = product.Tagline;

                        if (product.ShortDescription != null)
                            SessionSingleton.Current.SessionProduct.ShortDescription = product.ShortDescription;
                    }
                }
                else
                {
                    SessionSingleton.Current.SessionProduct.IsMarketPalce = false;

                }


                if (product.MessageAfterPayment != null)
                    SessionSingleton.Current.SessionProduct.MessageAfterPayment = product.MessageAfterPayment;

                SessionSingleton.Current.SessionProduct.UserId = db.aspnet_Users.First().UserId;

                if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                {
                    db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                }
                else
                {
                    if (SessionSingleton.Current.SessionIsEdit == true)
                    {
                        db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                    }
                    else
                    {
                        SessionSingleton.Current.SessionProduct.ProductId = Guid.NewGuid();
                        db.Products.Add(SessionSingleton.Current.SessionProduct);
                    }
                }
                try
                {
                    db.SaveChanges();
                }    // EXCEPTION!!!! WHAT 
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        { Debug.WriteLine(validationError.ErrorMessage); }
                    }

                    // rethrow, we want to fail this test if this happens
                    throw;
                }
                return RedirectToAction("GenerateButton");
            }

            return View(SessionSingleton.Current.SessionProduct);
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "SubmitMarketPlace")]
        public ActionResult SubmitMarketPlace(Product product)
        {
            if (ModelState.IsValid)
            {
                if (SessionSingleton.Current.SessionProduct.IsMarketPalce != null)
                {
                    if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                    {
                        if (SessionSingleton.Current.SessionProduct.Details != null)
                            product.Details = SessionSingleton.Current.SessionProduct.Details;

                        var item = db.Products.Where(p => p.ProductId == SessionSingleton.Current.SessionProduct.ProductId).First();
                        SessionSingleton.Current.SessionProduct = (Product)item;
                        SessionSingleton.Current.SessionProduct.IsMarketPalce = true;

                        if (product.Details != null)
                            SessionSingleton.Current.SessionProduct.Details = product.Details;

                        if (product.Category1 != null)
                            SessionSingleton.Current.SessionProduct.Category1 = product.Category1;

                        if (product.Category2 != null)
                            SessionSingleton.Current.SessionProduct.Category2 = product.Category2;

                        if (product.Category3 != null)
                            SessionSingleton.Current.SessionProduct.Category3 = product.Category3;

                        if (product.Category4 != null)
                            SessionSingleton.Current.SessionProduct.Category4 = product.Category4;

                        if (product.Category5 != null)
                            SessionSingleton.Current.SessionProduct.Category5 = product.Category5;

                        if (product.DetailsUrl != null)
                            SessionSingleton.Current.SessionProduct.DetailsUrl = product.DetailsUrl;

                        if (product.DemoUrl != null)
                            SessionSingleton.Current.SessionProduct.DemoUrl = product.DemoUrl;

                        if (product.Tagline != null)
                            SessionSingleton.Current.SessionProduct.Tagline = product.Tagline;

                        if (product.ShortDescription != null)
                            SessionSingleton.Current.SessionProduct.ShortDescription = product.ShortDescription;
                    }
                }
                else
                {
                    SessionSingleton.Current.SessionProduct.IsMarketPalce = false;

                }


                if (product.MessageAfterPayment != null)
                    SessionSingleton.Current.SessionProduct.MessageAfterPayment = product.MessageAfterPayment;

                SessionSingleton.Current.SessionProduct.UserId = db.aspnet_Users.First().UserId;

                if (SessionSingleton.Current.SessionProduct.IsMarketPalce == true)
                {
                    db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                }
                else
                {
                    if (SessionSingleton.Current.SessionIsEdit == true)
                    {
                        db.Entry(SessionSingleton.Current.SessionProduct).State = EntityState.Modified;
                    }
                    else
                    {
                        SessionSingleton.Current.SessionProduct.ProductId = Guid.NewGuid();
                        db.Products.Add(SessionSingleton.Current.SessionProduct);
                    }
                }
                try
                {
                    db.SaveChanges();
                }    // EXCEPTION!!!! WHAT 
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        { Debug.WriteLine(validationError.ErrorMessage); }
                    }

                    // rethrow, we want to fail this test if this happens
                    throw;
                }
                return RedirectToAction("LikeToDoNext");
            }

            return View(SessionSingleton.Current.SessionProduct);
        }
        //
        // POST: /Product/MoreOptions
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "MoreOptions")]
        public ViewResult MoreOptions(Product product)
        {
            SessionSingleton.Current.SessionProduct.ProductName = product.ProductName;
            SessionSingleton.Current.SessionProduct.IsSingleFile = product.IsSingleFile;
            SessionSingleton.Current.SessionProduct.IsRedirection = product.IsRedirection   ;
            SessionSingleton.Current.SessionProduct.IsStoreCode = product.IsStoreCode;
            SessionSingleton.Current.SessionProduct.IsGenCode = product.IsGenCode;
            SessionSingleton.Current.SessionProduct.IsAffectOrtherProducts = product.IsAffectOrtherProducts;
            SessionSingleton.Current.SessionProduct.IsInventoryControl = product.IsInventoryControl;
            SessionSingleton.Current.SessionProduct.IsTaxVAT = product.IsTaxVAT;
            SessionSingleton.Current.SessionProduct.IsShipOrBuyerAddress = product.IsShipOrBuyerAddress;
            SessionSingleton.Current.SessionProduct.IsEditQuantity = product.IsEditQuantity;
            SessionSingleton.Current.SessionProduct.IsEditPrice = product.IsEditPrice;
            SessionSingleton.Current.SessionProduct.IsVariants = product.IsVariants;
            SessionSingleton.Current.SessionProduct.IsVariations = product.IsVariations;
            SessionSingleton.Current.SessionProduct.IsSendTransaction = product.IsSendTransaction;
            SessionSingleton.Current.SessionProduct.Price = product.Price;
            SessionSingleton.Current.SessionProduct.EmailSubject = product.EmailSubject;
            SessionSingleton.Current.SessionProduct.EmailMessage = product.EmailMessage;
            SessionSingleton.Current.SessionProduct.IsReceiveCopy = product.IsReceiveCopy;
            SessionSingleton.Current.SessionProduct.IsEnableTemplateEmail = product.IsEnableTemplateEmail;
            SessionSingleton.Current.SessionProduct.ItemNumber = product.ItemNumber;
            return View(SessionSingleton.Current.SessionProduct);
        }

        //
        // POST: /Product/CustomizeThankYou
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "CustomizeThankYou")]
        public ViewResult CustomizeThankYou(Product product)
        {
            SessionSingleton.Current.SessionProduct.Expiration = product.Expiration;
            SessionSingleton.Current.SessionProduct.PINOrRegistrationCodeEncryption = product.PINOrRegistrationCodeEncryption;
            return View(SessionSingleton.Current.SessionProduct);
        }

        //
        // GET: /Product/GenerateButton
        [HttpGet]
        public ViewResult GenerateButton()
        {
            return View(SessionSingleton.Current.SessionProduct);
        }
        
        //
        // POST: /Product/BackProductConfiguration
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "BackProductConfiguration")]
        public ViewResult BackProductConfiguration(Product product)
        {
            SessionSingleton.Current.SessionProduct.Expiration = product.Expiration;
            SessionSingleton.Current.SessionProduct.PINOrRegistrationCodeEncryption = product.PINOrRegistrationCodeEncryption;
            return View("ProductConfiguration", SessionSingleton.Current.SessionProduct);
        }

        //
        // POST: /Product/BackMoreOptions
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "BackMoreOptions")]
        public ViewResult BackMoreOptions(Product product)
        {
            SessionSingleton.Current.SessionProduct.MessageAfterPayment = product.MessageAfterPayment;
            return View("MoreOptions", SessionSingleton.Current.SessionProduct);
        }

        //
        // POST: /Product/CategoryInformation
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "CategoryInformation")]
        public ViewResult CategoryInformation(Product product)
        {
            SessionSingleton.Current.SessionProduct.Details = product.Details;
            return View(SessionSingleton.Current.SessionProduct);
        }

        //
        // POST: /Product/BackRequiredInfomation
        [HttpPost]
        [MultipleButton(Name = "action", Argument = "BackRequiredInfomation")]
        public ViewResult BackRequiredInfomation(Product product)
        {
            SessionSingleton.Current.SessionProduct.Category1 = product.Category1;
            SessionSingleton.Current.SessionProduct.Category2 = product.Category2;
            SessionSingleton.Current.SessionProduct.Category3 = product.Category3;
            SessionSingleton.Current.SessionProduct.Category4 = product.Category4;
            SessionSingleton.Current.SessionProduct.Category5 = product.Category5;
            SessionSingleton.Current.SessionProduct.DetailsUrl = product.DetailsUrl;
            SessionSingleton.Current.SessionProduct.DemoUrl = product.DemoUrl;
            SessionSingleton.Current.SessionProduct.Tagline = product.Tagline;
            SessionSingleton.Current.SessionProduct.ShortDescription = product.ShortDescription;
            return View("RequiredInformation",SessionSingleton.Current.SessionProduct);
        }

        public ViewResult RequiredInformation(Guid? id)
        {
            SessionSingleton.Current.SessionProduct.IsMarketPalce = true;
            if (id != null)
            {
                Product product = db.Products.Find(id);
                SessionSingleton.Current.SessionProduct = product;
                SessionSingleton.Current.SessionIsEdit = true;
            }
            else
            {
                SessionSingleton.Current.SessionIsEdit = false;
            }
            return View(SessionSingleton.Current.SessionProduct);
        }

        //
        // GET: /Product/UploadImages
        [HttpGet]
        public ViewResult UploadImages()
        {
            return View();
        }

        [HttpGet]
        public ViewResult UploadImagesClose()
        {
            return View();
        }

        public ActionResult Upload(ImagesViewModel model)
        {
            Boolean flag = false;
            foreach (string upload in Request.Files)
            {
                if (!(Request.Files[upload]!=null && Request.Files[upload].ContentLength>0)) continue;
                var file = Request.Files[upload];
                String fileType = file.ContentType;
                int fileSize = file.ContentLength;
                
                if (upload.Equals("thumbnail"))
                {
                    if(fileType.Equals("image/jpeg") || fileType.Equals("image/gif"))
                    {
                        if (fileSize / 1024 < 20)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    else
                    {
                        flag = false;
                    }
                }

                if(upload.Equals("content") && flag)
                {
                    if (fileType.Equals("application/x-shockwave-flash") || fileType.Equals("image/jpeg") || fileType.Equals("image/gif"))
                    {
                        if (fileSize / 1024 < 100)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    else
                    {
                        flag = false;
                    }
                }
            }

            foreach (string upload in Request.Files)
            {
                if (!(Request.Files[upload] != null && Request.Files[upload].ContentLength > 0 && flag)) continue;
                    string path = AppDomain.CurrentDomain.BaseDirectory + "Content/products/images/";
                    string filename = Guid.NewGuid() + Path.GetFileName(Request.Files[upload].FileName);
                    Request.Files[upload].SaveAs(Path.Combine(path, filename));
                    if (upload.Equals("thumbnail"))
                    {
                        SessionSingleton.Current.SessionProduct.ImageFilePath = filename;
                    }

                    if (upload.Equals("content"))
                    {
                        
                    }
            }
            if (flag)
            {
                Product product = db.Products.Find(SessionSingleton.Current.SessionProduct.ProductId);
                product.ImageFilePath = SessionSingleton.Current.SessionProduct.ImageFilePath;
                db.Entry(product).State = EntityState.Modified;                   
                try
                {
                    db.SaveChanges();
                }    // EXCEPTION!!!! WHAT 
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        { Debug.WriteLine(validationError.ErrorMessage); }
                    }

                    // rethrow, we want to fail this test if this happens
                    throw;
                }

                return View("LikeToDoNext");
            }
            else
            {
                return View("UploadImages", model);
            }
        }

        [HttpGet]
        public ViewResult LikeToDoNext()
        {
            return View();
        }
    }
}